#include <SDL2/SDL.h>
#include <stdio.h>

// https://wiki.libsdl.org/SDL_GameControllerAddMapping?highlight=%28%5CbCategoryGameController%5Cb%29%7C%28CategoryEnum%29
// https://www.libsdl.org/release/SDL-1.2.15/docs/html/guideinput.html
// https://www.libsdl.org/release/SDL-1.2.15/docs/html/joystick.html
// https://www.libsdl.org/release/SDL-1.2.15/docs/html/eventstructures.html

// joystick event
/*
Name

SDL_JoyAxisEvent -- Joystick axis motion event structure
Structure Definition

typedef struct{
  Uint8 type;
  Uint8 which;
  Uint8 axis;
  Sint16 value;
} SDL_JoyAxisEvent;
Structure Data

type	SDL_JOYAXISMOTION
which	Joystick device index
axis	Joystick axis index
value	Axis value (range: -32768 to 32767)

Structure Definition

typedef struct{
  Uint8 type;
  Uint8 which;
  Uint8 button;
  Uint8 state;
} SDL_JoyButtonEvent;
Structure Data

type	SDL_JOYBUTTONDOWN or SDL_JOYBUTTONUP
which	Joystick device index
button	Joystick button index
state	SDL_PRESSED or SDL_RELEASED

Structure Definition

typedef struct{
  Uint8 type;
  Uint8 which;
  Uint8 hat;
  Uint8 value;
} SDL_JoyHatEvent;
Structure Data

type	SDL_JOY
which	Joystick device index
hat	Joystick hat index
value	Hat position


Structure Definition

typedef struct{
  Uint8 type;
  Uint8 which;SDL_JoystickName
  Uint8 ball;
  Sint16 xrel, yrel;
} SDL_JoyBallEvent;
Structure Data

type	SDL_JOYBALLMOTION
which	Joystick device index
ball	Joystick trackball index
xrel, yrel	The relative motion in the X/Y direction

 */

static void info_joystick()
{

	SDL_Joystick *joy;


	// Check for joystick
	printf("======= num sticks: %d\n", SDL_NumJoysticks());

	printf("%i joysticks were found.\n\n", SDL_NumJoysticks() );

	// Querying the Number of Available Joysticks
	printf("The names of the joysticks are:\n");
	for (int i = 0; i < SDL_NumJoysticks(); i++ )
	{
		printf("    %s\n", SDL_JoystickName(i));
	}

	if (SDL_NumJoysticks() > 0) {
		// Open joystick
		joy = SDL_JoystickOpen(0);

		if (joy)
		{
			printf("Opened Joystick 0\n");
			printf("Name: %s\n", SDL_JoystickName(0));
			printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joy));
			printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joy));
			printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joy));
			printf("Number of Hats: %d\n", SDL_JoystickNumHats(joy));
		}
		else
			printf("Couldn't open Joystick 0\n");

		// Close if opened
		// if(SDL_JoystickOpened(0))
		SDL_JoystickClose(joy);
	}
}

static void open_joystick()
{
	SDL_Joystick *joystick;
	SDL_JoystickEventState(SDL_ENABLE);
	joystick = SDL_JoystickOpen(0);

	SDL_Event event;
	/* Other initializtion code goes here */

	/* Start main game loop here */
	int quit = 0;
	while (!quit)
		while (SDL_PollEvent(&event))
		{
			printf("=======%d\n", event.type);
			switch (event.type)
			{
			case SDL_KEYDOWN:
				printf("event name SDL_KEYDOWN: val: %d\n", event.type);
				/* handle keyboard stuff here */
				break;

			case SDL_QUIT:
				quit = 1;
				printf("event name SDL_QUIT: val: %d\n", event.type);
				/* Set whatever flags are necessary to */
				/* end the main game loop here */
				break;
			case SDL_JOYAXISMOTION:  /* Handle Joystick Motion */
				printf("event name SDL_JOYAXISMOTION: val: %d\n", event.type);
				if ( ( event.jaxis.value < -3200 ) || (event.jaxis.value > 3200 ) )
				{
					/* code goes here */
				}
				//    break;
				// case SDL_JOYAXISMOTION:  /* Handle Joystick Motion */
				printf("event name SDL_JOYAXISMOTION: val: %d\n", event.type);
				if ( ( event.jaxis.value < -3200 ) || (event.jaxis.value > 3200 ) )
				{
					if ( event.jaxis.axis == 0)
					{
						/* Left-right movement code goes here */
					}

					if ( event.jaxis.axis == 1)
					{
						/* Up-Down movement code goes here */
					}
				}
				break;
			case SDL_JOYHATMOTION:  /* Handle Hat Motion */
				printf("event name SDL_JOYHATMOTION: type: %d, val: %d \n",
				       event.type, event.jhat.value);
				if ( event.jhat.value & SDL_HAT_UP )
				{
					/* Do up stuff here */
					printf("SDL_HAT_UP\n");
				}
				if ( event.jhat.value & SDL_HAT_LEFT )
				{
					printf("SDL_HAT_LEFT\n");
					/* Do left stuff here */
				}

				if ( (event.jhat.value & SDL_HAT_RIGHTDOWN) == SDL_HAT_RIGHTDOWN)
				{
					printf("SDL_HAT_RIGHTDOWN: %x\n", SDL_HAT_RIGHTDOWN);
					/* Do right and down together stuff here */
				}
				break;
			case SDL_JOYBUTTONDOWN:  /* Handle Joystick Button Presses */
				printf("event name SDL_JOYBUTTONDOWN: type: %d, button: %d\n", 
					event.type, event.jbutton.button);
				if ( event.jbutton.button == 0 )
				{
					/* code goes here */
				}
				break;
			case SDL_JOYBALLMOTION:  /* Handle Joyball Motion */
				printf("event name SDL_JOYBALLMOTION: val: %d\n", event.type);
				if ( event.jball.ball == 0 )
				{
					/* ball handling */
				}
				break;
			}
		}

	SDL_JoystickClose(joystick);

}

// #define SDL_JoystickOpen
int main(int argc, char const *argv[])
{

	//我们要渲染的窗口
	SDL_Window* window = NULL;

	if (SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) < 0)
	{
		fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
		exit(1);
	}

	//创建窗口
	window = SDL_CreateWindow("MY FIRST SDL WINDOW", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 33, 333, SDL_WINDOW_SHOWN);
	if (NULL == window)
	{
		SDL_Quit();
		return -1;
	}
	info_joystick();

	open_joystick();

	// while(1)
	//等待两秒
	// SDL_Delay(2000);

	SDL_Quit();
	return 0;
}